import java.util.*;

class Solution1 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        Queue<Node> q = new LinkedList<>();
        if(root == null) return ret;
        q.add(root);

        while(!q.isEmpty()) {
            int sz = q.size();
            List<Integer> tmp = new ArrayList<>();
            for(int i = 0; i < sz; i++) {
                Node p = q.poll();
                tmp.add(p.val);
                for(Node node : p.children) {
                    if(node != null) {
                        q.add(node);
                    }
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
}

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};